﻿@namespace ThingsGateway.Components
@using System.Globalization;
@using Masa.Blazor.Presets;
@using System.Text.RegularExpressions
@using ThingsGateway.Components;
<PPageTabs @ref="PPageTabs" SelfPatterns="@SelfPatterns" AskBeforeClosing=false
           TabOptions=@TabOptions TabClass="px-6 rounded-t-xl" Ripple="false">
    <ChildContent>
        @ChildContent
    </ChildContent>
</PPageTabs>


@code{
    /// <summary>
    /// 子组件
    /// </summary>
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    /// <summary>
    /// PageTabItems
    /// </summary>
    [Parameter]
    public List<PageTabItem> PageTabItems { get; set; }

    /// <summary>
    /// Tabs实例
    /// </summary>
    public PPageTabs PPageTabs { get; private set; }

    /// <summary>
    /// SelfPatterns
    /// </summary>
    [Parameter]
    public IEnumerable<string> SelfPatterns { get; set; }

    private TabOptions TabOptions(PageTabPathValue value)
    {
        var item = PageTabItems.FirstOrDefault(u => IsMatch(u.Href, value.AbsolutePath));
        var title = item?.Title;
        var icon = item?.Icon;
        var titleClass = $"mx-2 text-capitalize {(value.Selected ? "primary--text" : "")}";
        var op = new TabOptions(title, icon, titleClass)
            {
                TitleStyle = "min-width:36px;",
            };
        return op;
    }

    private bool IsMatch(string input, string absolutePath)
    {
        if (!input.StartsWith("/"))
        {
            input = "/" + input;
        }
        string pattern = $@"^{input}(/.*)?$";
        bool isMatch = Regex.IsMatch(absolutePath, pattern, RegexOptions.IgnoreCase);
        return isMatch;
    }
}